<template>
  <el-form
    ref="formRef"
    label-width="100px"
    label-position="right"
    :model="formData"
    :rules="formRules"
  >
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="formData.name" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="formData.code" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="formData.manager" placeholder="请选择部门负责人">
        <el-option v-for="item in list" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="formData.introduce" />
    </el-form-item>
    <el-form-item label-width="40%">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="doSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeeSimple, addDept, getDeptInfo, updateDept } from '@/api/department'
export default {
  props: {
    pid: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: { // 表单要校验的数据
      type: Array,
      required: true
    }
  },
  // 1.数据
  data() {
    // 定义code校验规则
    const validCode = (rules, value, callback) => {
      // (1)把code获取出来
      let codeList = this.originList.map(item => item.code)
      // 如果点击的是编辑,要从数组中过滤 到自己
      if (this.isEdit) {
        codeList = this.originList.filter(item => item.id !== this.pid).map(item => item.code)
      }
      codeList.includes(value) ? callback(new Error(value + '已被占用')) : callback()
    }
    // 定义名字校验规则
    const validName = (rules, value, callback) => {
      let nameList = this.originList.filter(item => item.pid === this.pid).map(item => item.name)
      // // 如果点击的是编辑,则不能取的名字是: 当前部门的兄弟部门 - 它自己的名字
      if (this.isEdit) {
        const item = this.originList.find(item => item.id === this.pid)
        const pid = item.pid
        nameList = this.originList.filter(item => item.pid === pid && item.id !== this.pid).map(item => item.name)
        console.log(nameList)
      }
      nameList.includes(value) ? callback(new Error(value + '已被占用')) : callback()
    }
    return {
      // 1.1 表单的数据
      formData: {
        code: '', // 部门编码
        introduce: '', // 介绍
        manager: '', // 负责任
        name: '' // 部门名称
      },
      // 1.2下拉菜单数据
      list: [],
      // 1.3表单校验规则
      formRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  // 2.created
  created() {
    // 2.1获取员工下拉数据
    this.getSimple()
    // // 2.2编辑按钮,获取数据
    // if (this.isEdit) {
    //   this.deptInfo()
    // }
  },
  // 3.方法
  methods: {
    // 3.1获取员工下拉数据
    async getSimple() {
      const res = await getEmployeeSimple()
      this.list = res.data
    },
    // 3.2确定按钮
    async doSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.isEdit ? this.deptEdit() : this.deptAdd()
        }
      })
    },
    // 3.3编辑部门,获取当前的数据
    async deptInfo() {
      // 获取当前id的信息
      const res = await getDeptInfo(this.pid)
      this.formData = res.data
    },
    // 3.4添加部门
    async deptAdd() {
      await addDept({ ...this.formData, pid: this.pid })
      // 子传父
      this.$emit('reset', '新增成功')
    },
    // 3.5编辑部门
    async deptEdit() {
      // 调用编辑的请求
      await updateDept(this.formData)
      // 子传父
      this.$emit('reset', '编辑成功')
      this.formData = {
        code: '', // 部门编码
        introduce: '', // 介绍
        manager: '', // 负责任
        name: '' // 部门名称
      }
    },
    // 3.6取消按钮
    close() {
      this.$emit('hClose')
    },
    // 3.7清空表单数据
    formReset() {
      this.$refs.formRef.resetFields()
    }
  }
}
</script>

<style>

</style>
